﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

using CE.User;
using CE.Mail;

public partial class user_password_recovery : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session[GlobalSettings.CaptchaImageText] = GlobalSettings.GenerateRandomCode(4);
            hidSecurityCheck.Value = Session[GlobalSettings.CaptchaImageText].ToString();
        }
    }

    protected void btnRecoverPassword_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (txtSecurityCheck.Text.Trim().ToUpper() == hidSecurityCheck.Value.ToUpper())
            {
                AccountDB _accountdb = new AccountDB();
                Account _account = _accountdb.GetUserAccount(txtEmail.Text);
                if (_account != null)
                {
                    string salt = CE.Security.MySecurity.Encrypt(hidSecurityCheck.Value, true);
                    string strGUID = _accountdb.GetGUID(_account.UID);
                    salt = salt.Replace("+", "");
                    _account.AuthSalt = salt;
                    if (_accountdb.Insert(_account) > 0)
                    {
                        if (MyMailManager.SendPasswordResetLink(_account.Email, _account.Email, salt, strGUID, CE.Global.CEUserType.Customer))
                        {
                            lblSentMessage.Text = "Password recovery information has been sent to your email. Please check email inbox now.";
                            divForm.Visible = false;
                            divMessage.Visible = true;
                        }
                        else
                        {
                            lblMessage.Text = "Can't send password recovery information to your email account.!";
                        }
                    }
                    else
                    {
                        lblMessage.Text = "Creating password recovery information failed!Try again later!";
                    }
                }
                else
                {
                    lblMessage.Text = "No such email found.!";
                }
            }
            else
            {
                lblMessage.Text = "Wrong security check code. Please try again!";
                lblMessage.Font.Bold = true;
                lblMessage.ForeColor = System.Drawing.Color.Red;

                Session[GlobalSettings.CaptchaImageText] = GlobalSettings.GenerateRandomCode(4);
                hidSecurityCheck.Value = Session[GlobalSettings.CaptchaImageText].ToString();
            }
        }
    }
}
